#include "app_common.h"
#include "pthread.h"

void *fun1(void *args)
{
    int cnt = 0;
    while (1)
    {
        log_info("thread1....");
        cnt++;
        if (cnt == 10)
        {
            log_info("thread1 exit");
            pthread_exit(NULL);
        }
        sleep(1);
    }
}

void *fun2(void *args)
{
    sleep(1);

    log_info("thread2....");
}

int main(int argc, char const *argv[])
{
    pthread_t t1;
    pthread_t t2;

    int res = pthread_create(&t1, NULL, fun1, NULL);
    if (res != 0)
    {
        log_error("thead1 error");
    }
    log_info("thead1 success");
    res = pthread_create(&t2, NULL, fun2, NULL);
    if (res != 0)
    {
        log_error("thead2 error");
    }
    log_info("thead2 success");

    sleep(5);
    pthread_cancel(t1);

    // 等地子线程结束之后, 主线程再结束
    pthread_join(t1, NULL);
    pthread_join(t2, NULL);

    return 0;
}
/*
线程退出:
    1. 现在函数执行完毕,自动退出
    2. 线程内部调用 pthread_exit()
    3. 线程外部调用 pthread_cancel()


*/
